package com.snail.app.push;

import com.alibaba.druid.util.StringUtils;
import com.snail.common.ResultUtil;
import com.snail.common.entity.BaseController;
import com.snail.common.util.StringUtil;
import com.snail.app.gather.service.DataPushCommonService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @description
 * @date 2021/11/18
 * @autor houyanfeng
 */
@RestController
@RequestMapping("push/api")
public class DataPushApiController extends BaseController {
    @Autowired
    private DataPushCommonService dataPushCommonService;

    /** 通用数据抓取并上传平台方法 */
    @ApiImplicitParams({
        @ApiImplicitParam(name = "tableKey", value = "表名关键字，如'hd31_'加库表小写名", dataType = "", paramType = "query", example = "hd31_warehouse")
        ,@ApiImplicitParam(name = "sendId", value = "发送路径ID", dataType = "string", paramType = "query", required = false, example = "h6-pda-4101")
    })
    @GetMapping
    public Map<String,Object> pushAPI(@RequestParam String tableKey){
        Map<String,Object> params = new HashMap<>();
        if(!StringUtil.isEmpty(request.getParameter("startTime"))){
            params.put("startTime", request.getParameter("startTime"));
        }
        if(!StringUtil.isEmpty(request.getParameter("endTime"))){
            params.put("endTime", request.getParameter("endTime"));
        }
        if(!StringUtil.isEmpty(request.getParameter("batch"))){
            params.put("batch", request.getParameter("batch"));
        }
        try {
            if(!StringUtils.isEmpty(request.getParameter("sendId"))){
                dataPushCommonService.pushByApi(params, tableKey, request.getParameter("sendId"));
            }else {
                dataPushCommonService.fetchAndPushByAPI(params, tableKey);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(400, e.getMessage(), null);
        }
        return ResultUtil.result();
    }
}
